﻿using lingopen.Application.System;
using lingopen.Entity.System;
using lingopen.Extensions;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace lingopen.Jabil.Server.Controllers
{
    /// <summary>
    /// 主页
    /// </summary>
    [LoginAuthorizeAttribute]
    public class AdminController : WebController
    {
        /// <summary>
        /// 主页
        /// </summary>
        /// <returns></returns>
        // GET: Home
        public ActionResult Index()
        {
            return RedirectToAction("Index");
            //return Redirect("/start/index.html");
        }
        
        /// <summary>
        /// 默认页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Default()
        {
            this.ViewBag.CurrentSession = this.CurrentSession;
            return View();
        }

        List<object> ToMenuJson(List<Sys_Module> data, string parentId)
        {
            List<object> ret = new List<object>();
            List<Sys_Module> entities = data.FindAll(t => t.ParentId == parentId);
            if (entities.Count > 0)
            {
                foreach (var item in entities)
                {
                    JObject menu = new JObject();
                    menu.Add("title", item.Name);
                    if (item.Icon != null)
                    {
                        menu.Add("icon", item.Icon);
                    }
                    if (item.OpenTarget == "expand")
                    {
                        menu.Add("list", JToken.FromObject(ToMenuJson(data, item.Id)));
                    }
                    if (item.OpenTarget == "blank")
                    {
                        menu.Add("blank", true);
                    }
                    else
                    {
                        menu.Add("blank", false);
                    }
                    if (item.OpenTarget == "iframe")
                    {
                        menu.Add("jump", item.UrlAddress);
                    }
                    else
                    {
                        menu.Add("name", item.UrlAddress);
                    }
                    ret.Add(menu);

                }
            }
            return ret;
        }
        /// <summary>
        /// 左侧菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult LeftMenu()
        {

            var roleId = this.CurrentSession.RoleId;
            var service = this.CreateService<IRoleAuthorizeAppService>();
            List<Sys_Module> menuList = service.GetMenuList(roleId);

            //menuList.ForEach(a =>
            //{
            //    if (a.UrlAddress.IsNotNullOrEmpty())
            //    {
            //        a.UrlAddress = this.Url.Content(a.UrlAddress);
            //    }
            //});

            var data = ToMenuJson(menuList, null); //递归
            ////添加额外的演示菜单
            //data.Add(new
            //{
            //    name = "layuiAdmin",
            //    title = "layuiAdmin",
            //    icon = "layui-icon-component",
            //    jump = "/iframe/link/layuiAdmin"
            //});
            return UISuccessData(data);
            //return this.JsonContent(new
            //{
            //    code = 0,
            //    msg = "",
            //    data = new List<object>(){
            //        new {
            //            name="component",
            //            title="组件",
            //            icon="layui-icon-component",
            //            list = new List<object>(){
            //                new {
            //                    name="button",
            //                    title="按钮"
            //                }
            //            }
            //        },
            //        new{
            //            name = "layuiAdmin",
            //            title = "layuiAdmin",
            //            icon="layui-icon-component",
            //            jump = "/iframe/link/layuiAdmin"
            //        }
            //    }
            //});
        }
        /// <summary>
        /// 获取客户端数据
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public ActionResult GetClientsDataJson()
        {
            var data = new
            {
                //dataItems = this.GetDataItemList(),
                //organize = this.GetOrganizeList(),
                //role = this.GetRoleList(),
                //duty = this.GetDutyList(),
                user = "",
                authorizeMenu = this.GetMenuList(),
                //authorizeButton = this.GetMenuButtonList(),
            };
            return this.SuccessData(data);
        }
        /// <summary>
        /// 报错
        /// </summary>
        /// <returns></returns>
        [SkipLoginAuthorizeAttribute]
        public ActionResult Error()
        {
            return this.View();
        }

        object GetMenuList()
        {
            var roleId = this.CurrentSession.RoleId;
            var service = this.CreateService<IRoleAuthorizeAppService>();
            List<Sys_Module> menuList = service.GetMenuList(roleId);

            menuList.ForEach(a =>
            {
                if (a.UrlAddress.IsNotNullOrEmpty())
                {
                    a.UrlAddress = this.Url.Content(a.UrlAddress);
                }
            });

            return this.ToMenuJson(menuList, null);
        }
        //string ToMenuJson(List<Sys_Module> data, string parentId)
        //{
        //    StringBuilder sbJson = new StringBuilder();
        //    sbJson.Append("[");
        //    List<Sys_Module> entities = data.FindAll(t => t.ParentId == parentId);
        //    if (entities.Count > 0)
        //    {
        //        foreach (var item in entities)
        //        {
        //            string strJson = JsonHelper.Serialize(item);
        //            strJson = strJson.Insert(strJson.Length - 1, ",\"ChildNodes\":" + ToMenuJson(data, item.Id) + "");
        //            sbJson.Append(strJson + ",");
        //        }
        //        sbJson = sbJson.Remove(sbJson.Length - 1, 1);
        //    }
        //    sbJson.Append("]");
        //    return sbJson.ToString();
        //}
    }
}